﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Microsoft.Reporting.WebForms;
using System.Data;
using Viewer.Base.Config;
using Viewer.BusinessObject.BusinessEntities;

namespace Viewer.NhanSu.Reporting
{
    public partial class RptCoCauLaoDong : System.Web.UI.Page
    {
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataComboboxLoai();
        }
        protected void MasterScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
        {
            // TODO: see if this is ever invoked
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            var MasterScriptManager = (ScriptManager)this.FindControl("MasterScriptManager");
            MasterScriptManager.EnablePartialRendering = false;
        }
        [DirectMethod]
        public void btnGenerate_Click()
        {
            string reportPath = string.Empty;
            if (hdReportType.Value.Equals("0") || hdReportType.Value.Equals("1") || hdReportType.Value.Equals("4"))
            {
                if (string.IsNullOrEmpty(txtToChuc.Text))
                {
                    hdTochucID.Text = string.Empty;
                }
                string toChucId = !string.IsNullOrEmpty(hdTochucID.Text) ? hdTochucID.Text : string.Empty;
                // Configuration setting from Reports.config
                // Path for Server Report

                if (hdReportType.Value.Equals("0"))
                {
                    reportPath = ViewerConstant.NameReport.NSNgheNghiep;
                }
                else if (hdReportType.Value.Equals("1"))
                {
                    reportPath = ViewerConstant.NameReport.NSNgachCongChuc;
                }
                else if (hdReportType.Value.Equals("4"))
                {
                    reportPath = ViewerConstant.NameReport.NSChuyenNganh;
                }
                string strParameter = string.Format("{0},{1},{2},{3}", reportPath, txtToChuc.Text, hdTochucID.Value.ToString(), cbbYear.Text);
                UCReporting.LoadReport(strParameter);
            }
            else
            {
                if (CheckValidate())
                {
                    //lblMessage.Hidden = true;
                    if (string.IsNullOrEmpty(txtToChuc.Text))
                    {
                        hdTochucID.Text = string.Empty;
                    }

                    string toChucId = !string.IsNullOrEmpty(hdTochucID.Text) ? hdTochucID.Text : string.Empty;
                    List<ReportParameter> parameters = new List<ReportParameter>();
                    string strParameter = string.Empty;
                    // Configuration setting from Reports.config
                    // Path for Server Report

                    if (hdReportType.Value.Equals("2"))
                    {
                        reportPath = ViewerConstant.NameReport.NSBaoCaoNgach;
                        PnlSearch_Ngach.Hidden = false;
                        strParameter = string.Format("{0},{1},{2},{3},{4},{5}", reportPath, toChucId, txtCodeNgach.Value.ToString(), txtTenNgach.Text, cbbFrom_Year.Text, cbbEnd_Year.Text);
                    }
                    else
                    {
                        reportPath = ViewerConstant.NameReport.NSBaoCaoNghe;
                        PnlSearch_NgheNghiep.Hidden = false;
                        strParameter = string.Format("{0},{1},{2},{3},{4},{5}", reportPath, toChucId, txtCodeNghe.Value.ToString(), txtTenNgheNghiep.Text, cbbFromYear_Nghe.Text, cbbEndYear_Nghe.Text);
                    }

                    UCReporting.LoadReport(strParameter);
                }
                if (hdReportType.Value.Equals("2"))
                {

                    PnlSearch_Ngach.Hidden = false;
                    PnlSearch_NgheNghiep.Hidden = true;
                }
                else
                {
                    PnlSearch_Ngach.Hidden = true;
                    PnlSearch_NgheNghiep.Hidden = false;
                }
            }
          
        }
        [DirectMethod]
        public void btnReset_Click()
        {
            if (hdReportType.Value.Equals("0") || hdReportType.Value.Equals("1"))
            {
                txtToChuc.Text = string.Empty;
                hdTochucID.Text = string.Empty;

                cbbYear.Clear();
                SetHidden(false, true, true);
            }
            else if (hdReportType.Value.Equals("2"))
            {
                txtToChuc_Ngach.Text = string.Empty;
                hdTochucID.Text = string.Empty;

                cbbEnd_Year.Clear();
                cbbFrom_Year.Clear();
                txtTenNgach.Text = string.Empty;
                hdNgachCongChucId.Value = string.Empty;
                txtCodeNgach.Text = string.Empty;
                SetHidden(true, false, true);
            }
            else
            {
                txtToChuc_NgheNghiep.Text = string.Empty;
                hdTochucID.Text = string.Empty;
                cbbEndYear_Nghe.Clear();
                cbbFromYear_Nghe.Clear();
                txtTenNgheNghiep.Text = string.Empty;
                hdNgheNghiepId_LookUp.Value = string.Empty;
                txtCodeNghe.Text = string.Empty;
                SetHidden(true, true, false);
            }
           
        }
        /// <summary>
        /// Check validate control
        /// </summary>
        /// <returns></returns>
        private bool CheckValidate()
        {
            bool check = true;
            if (hdReportType.Value.Equals("2"))
            {
                if (string.IsNullOrEmpty(txtTenNgach.Text))
                {
                    lblMessage_Ngach.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessage_Ngach.Hidden = true;
                }
                if (string.IsNullOrEmpty(cbbEnd_Year.SelectedItem.Text))
                {
                    lblMessageEnd.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessageEnd.Hidden = true;
                }
                if (string.IsNullOrEmpty(cbbFrom_Year.SelectedItem.Text))
                {
                    lblMessageFrom.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessageFrom.Hidden = true;
                }
                PnlSearch_Ngach.Hidden = false;
            }
            else
            {
                if (string.IsNullOrEmpty(hdNgheNghiepId_LookUp.Value.ToString()))
                {
                    lblMessage_Nghe.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessage_Nghe.Hidden = true;
                }
                if (string.IsNullOrEmpty(cbbEndYear_Nghe.SelectedItem.Text))
                {
                    lblMessageEnd_Nghe.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessageEnd_Nghe.Hidden = true;
                }
                if (string.IsNullOrEmpty(cbbFromYear_Nghe.SelectedItem.Text))
                {
                    lblMessageFrom_Nghe.Hidden = false;
                    check = false;
                }
                else
                {
                    lblMessageFrom_Nghe.Hidden = true;
                }
                PnlSearch_NgheNghiep.Hidden = false;
            }
            return check;
        }
        /// <summary>
        /// Load data cho commbox năm theo xml
        /// </summary>
        private void LoadDataComboboxLoai()
        {
            string xmlFile = Server.MapPath(ViewerConstant.FILEPATH_Report_XML);
            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);
            storeNam.DataSource = ds.Tables["Nam"];
            storeNam.DataBind();
        }

        [DirectMethod]
        public void ShowPopUp()
        {
            UcToChuc.ShowLookUp_ToChuc("", true, "");
        }
        [DirectMethod]
        public void SetHidden(bool check, bool check1, bool check2)
        {
            PnlSearch.Hidden = check;
            PnlSearch_Ngach.Hidden = check1;
            PnlSearch_NgheNghiep.Hidden = check2;
        }
        [DirectMethod]
        public void FindCommonName_NgachCongChuc(string codeString)
        {
            CHCommonCode obj = common.LoadCommonCodeByCodeString(codeString, common.NgachCongChuc);
            if (obj != null)
            {
                txtTenNgach.Text = obj.CodeValue;
                hdNgachCongChucId.Text = obj.CodeID.ToString();

            }
            else
            {
                txtTenNgach.Text = string.Empty;
                hdNgachCongChucId.Text = string.Empty;
            }

        }
        [DirectMethod]
        public void LookUp_TimNgach()
        {
            UCTimNgach.ShowDialogTimNgach(common.NgachCongChuc);
        }
        [DirectMethod]
        public void FindCommonName_NgheNghiep(string codeString)
        {
            CHCommonCode obj = common.LoadCommonCodeByCodeString(codeString, common.NgheNghiep);
            if (obj != null)
            {
                txtCodeNghe.Text = obj.CodeString;
                txtTenNgheNghiep.Text = obj.CodeValue;
                hdNgheNghiepId_LookUp.Value = obj.CodeID;
            }
            else
            {
                txtCodeNghe.Text = string.Empty;
                txtTenNgheNghiep.Text = string.Empty;
            }

        }
        [DirectMethod]
        public void LookUp_TimNghe()
        {
            UCTimNghe.ShowDialogTimNghe(common.NgheNghiep);
        }
    }
}